//
//  CellForSettings.m
//  ProjectA
//
//  Created by dllo on 16/1/25.
//  Copyright © 2016年 刘旺辉. All rights reserved.
//

#import "CellForSettings.h"
#import "AppMacro.h"
#import "CheckPhone.h"
@interface CellForSettings ()
@property (nonatomic, strong) UIImageView *imageViewForleft;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) UISwitch *rightSwitch;
@end

@implementation CellForSettings

- (void)dealloc {
    [_imageViewForleft release];
    [_label release];
    [_rightSwitch release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.imageViewForleft = [[UIImageView alloc] init];
        [self.contentView addSubview:self.imageViewForleft];
        [_imageViewForleft release];
        
        self.label = [[UILabel alloc] init];
        [self.contentView addSubview:self.label];
        [_label release];
        
        self.rightSwitch = [[UISwitch alloc] init];
        [self.rightSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        [self.contentView addSubview:self.rightSwitch];
        [_rightSwitch release];
        
        //判断上一次登陆时的偏好设置
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        BOOL isNight = [user boolForKey:@"IsNight"];
        [self.rightSwitch setOn:isNight];
        NSLog(@"%@", NSHomeDirectory());
        
        
        
        
    }
    return self;
}

- (void)switchAction:(UISwitch *)rightSwitch {
    
    
    NSLog(@"%d", rightSwitch.on);
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    
    if (rightSwitch.on == YES) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:kNightDayModelNotification object:nil];
        [user setBool:YES forKey:@"IsNight"];
        [user synchronize];
        self.label.textColor = L_COLOR_WHITE;
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:kLightDayModelNotification object:nil];
        [user setBool:NO forKey:@"IsNight"];
        [user synchronize];
        self.label.textColor = L_COLOR_BLACK;
    }
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    
    
    /** 判断手机型号 */
    NSString *iphone = [CheckPhone getCurrentDevice];
    if ([iphone isEqualToString:@"iPhone 6 Plus (A1522/A1524)"]) {
    
        self.imageViewForleft.frame = CGRectMake(25, 6, 30, 30);
        self.label.frame = CGRectMake(15 + 30 + 10 + 5 + 2 + 10 + 6, 10, 80, self.frame.size.height - 20);
        self.label.text = @"夜间模式";
        self.rightSwitch.frame = CGRectMake(self.frame.size.width - (self.frame.size.width / 7.f + 10), 10, self.frame.size.width / 7.f, self.frame.size.height - 20);
        
    } else {
        
        self.imageViewForleft.frame = CGRectMake(15, 10, 30, 30);
        self.label.text = @"夜间模式";
        self.label.frame = CGRectMake(15 + 30 + 10 + 5 + 2, 10, 80, self.frame.size.height - 20);
        self.label.font = [UIFont systemFontOfSize:17];
        self.rightSwitch.frame = CGRectMake(self.frame.size.width - (self.frame.size.width / 7.f + 10), 10, self.frame.size.width / 7.f, self.frame.size.height - 20);
        
    }
    self.imageViewForleft.image = [UIImage imageNamed:@"iconfont-yueliang"];
    [UIView setColorWithLabelText:self.label nightColor:L_COLOR_WHITE dayColor:L_COLOR_BLACK];
}







- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
